package cn.lg.soar.system.biz.modules.auth.model;

import cn.lg.soar.core.annotations.constraints.group.Create;
import cn.lg.soar.core.annotations.constraints.group.Update;
import cn.lg.soar.system.biz.modules.auth.entity.TemporaryPassword;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.Setter;

import java.time.LocalDateTime;

/**
 * 临时密码
 * @author luguoxiang
 * @date 2021/9/12
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@Getter
@Setter
public class TemporaryPasswordVo {

    @NotNull(message = "id不能为空", groups = Update.class)
    private Long id;

    @NotBlank(message = "密码不能为空", groups = Create.class)
    @JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
    @Schema(description = "临时密码")
    private String password;

    @Schema(description = "有效期，为空表示不限")
    private LocalDateTime expires;

    @Schema(description = "最多允许次数，0表示不限")
    private Integer maxCount;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "已使用次数")
    private Integer usageCount;

    @Schema(description = "备注")
    private String remark;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "最近一次登录时间")
    private LocalDateTime lastLoginDate;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "更新时间")
    private LocalDateTime updateTime;

    @JsonProperty(access = JsonProperty.Access.READ_ONLY)
    @Schema(description = "创建时间")
    private LocalDateTime createTime;

    public TemporaryPasswordVo() {
    }

    public TemporaryPasswordVo(TemporaryPassword source) {
        this.id = source.getId();
        this.password = source.getPassword();
        this.expires = source.getExpires();
        this.maxCount = source.getMaxCount();
        this.usageCount = source.getUsageCount();
        this.remark = source.getRemark();
        this.lastLoginDate = source.getLastLoginDate();
        this.updateTime = source.getUpdateTime();
        this.createTime = source.getCreateTime();
    }

    public TemporaryPassword toTemporaryPassword() {
        TemporaryPassword target = new TemporaryPassword();

        target.setId(this.id);
        target.setPassword(this.password);
        target.setExpires(this.expires);
        target.setMaxCount(this.maxCount);
        target.setUsageCount(this.usageCount);
        target.setRemark(this.remark);
        target.setLastLoginDate(this.lastLoginDate);
        target.setUpdateTime(this.updateTime);
        target.setCreateTime(this.createTime);
        return target;
    }
}
